package com.wulinbus.controller.admin;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fancy.common.utils.StringUtil;
import com.wulinbus.model.CreativeHelperModel;
import com.wulinbus.model.CreativeModel;
import com.wulinbus.model.RechargeOrderModel;
import com.wulinbus.model.RemitOrderModel;
import com.wulinbus.model.RenderProviderModel;
import com.wulinbus.model.ScriptModel;
import com.wulinbus.model.ScriptOwnModel;
import com.wulinbus.model.UserCashLogModel;
import com.wulinbus.model.UserModel;
import com.wulinbus.service.CreativeHelperService;
import com.wulinbus.service.CreativeOrderService;
import com.wulinbus.service.CreativeService;
import com.wulinbus.service.RechargeOrderService;
import com.wulinbus.service.RemitOrderService;
import com.wulinbus.service.RenderService;
import com.wulinbus.service.ScriptService;
import com.wulinbus.service.UserCashLogService;
import com.wulinbus.service.UserService;
import com.wulinbus.utils.AdminAuthCheck;

@Controller
@AdminAuthCheck
@RequestMapping("/admin")
public class AdminUserController {

	@Resource
	private UserService userService;

	@Resource
	private ScriptService scriptService;

	@Resource
	private CreativeService creativeService;

	@Resource
	private CreativeOrderService creativeOrderService;

	@Resource
	private UserCashLogService userCashLogService;

	@Resource
	private CreativeHelperService creativeHelperService;
	
	@Resource
	private RenderService renderService;
	@Resource
	private RechargeOrderService rechargeOrderService;
	@Resource
	private RemitOrderService remitOrderService;

	@RequestMapping(value = { "", "/" })
	public String home(@CookieValue(value="muid",defaultValue="") String muid) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
			return "admin/index"; 
		}
	}

	@RequestMapping("index.html")
	public String index(@CookieValue(value="muid",defaultValue="") String muid) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
			return "admin/index"; 
		}
	}

	@RequestMapping("/user_list.html")
	public String user_list(@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
			model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
			List<UserModel> userList = userService.getList(start, pageSize);
			model.addAttribute("userList", userList);
			model.addAttribute("totalCount", userService.getListCount());
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/user_list";
		}
	}

	@RequestMapping("/user_cash.html")
	public String user_cash(@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
			model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
			List<UserCashLogModel> logList = userCashLogService.getList(start, pageSize);
			model.addAttribute("logList", logList);
			model.addAttribute("totalCount", userCashLogService.getListCount());
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/user_cash";
		}
	}

	@RequestMapping(value = "/cash_post.json", method = { RequestMethod.POST })
	public void user_cash_post(Model model, Integer uid, Integer lid, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		if (userCashLogService.updateState(lid, uid) > 0) {
			response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
		} else {
			response.getWriter().print("{\"message\":\"操作失败\",\"statusCode\":\"300\"}");
		}

	}
	/**
	 * 创意审核
	 */
	@RequestMapping("/creative_check.html")
	public String creative_check(String keyword,@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{   
		 	model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
		 	List<CreativeModel> creativeList = creativeService.getCreativeList(keyword, start, pageSize);
		 	model.addAttribute("creativeList",creativeList);
		 	model.addAttribute("totalCount",creativeService.count(keyword));
		  	model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/creative_check";
		}
	}

	/**
	 * 剧本审核
	 */
	@RequestMapping("/script_check.html")
	public String script_check(String keyword,@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{   
		 	model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
		 	List<ScriptModel> scriptList = scriptService.getScriptList(keyword, start, pageSize);
		 	model.addAttribute("scriptList",scriptList);
		 	model.addAttribute("totalCount",scriptService.count(keyword));
		  	model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/script_check";
		}
	}
	/**
	 * 投稿审核
	 */
	@RequestMapping("/attribute_check.html")
	public String attribute_check(String keyword,@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{   
		 	model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
		 	List<ScriptOwnModel> attributeList = scriptService.getScriptOwnList(keyword, start, pageSize);
		 	model.addAttribute("attributeList",attributeList);
		 	model.addAttribute("totalCount",scriptService.ScriptOwnCount(keyword));
		  	model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/attribute_check";
		}
	}
	/**
	 * 创意助手列表
	 */
	@RequestMapping("/creative_helper_list.html")
	public String creative_helper_list(String keyword,@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{   
		 	model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
		 	List<CreativeHelperModel> creativeHelperList = creativeHelperService.getCreativeHelperList(keyword, start, pageSize);
		 	model.addAttribute("creativeHelperList",creativeHelperList);
		 	model.addAttribute("totalCount",creativeHelperService.creativeHelperCount(keyword));
		  	model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/creative_helper_list";
		}
	}
	/**
	 * 渲染提供商列表
	 */
	@RequestMapping("/render_provider_list.html")
	public String render_provider_list(String keyword,@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{   
		 	model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
		 	List<RenderProviderModel> renderProviderList = renderService.getRenderProviderList(keyword, start, pageSize);
		 	model.addAttribute("renderProviderList",renderProviderList);
		 	model.addAttribute("totalCount",renderService.RenderProviderCount(keyword));
		  	model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/render_provider_list";
		}
	}
	/**
	 * 用户充值记录
	 */
	@RequestMapping("recharge_list.html")
	public String recharge_list(String keyword,@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{   
		 	model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
		 	List<RechargeOrderModel> rechargeOrderList = rechargeOrderService.getRechargeOrderList(keyword, start, pageSize);
		 	model.addAttribute("rechargeOrder",rechargeOrderList);
		 	model.addAttribute("totalCount",rechargeOrderService.allCount(keyword));
		  	model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/user_recharge_list";
		}
	}
	/**
	 * 剧本打款记录
	 */
	@RequestMapping("script_automatic_remit.html")
	public String script_automatic_remit(String keyword,@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{   
		 	model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
		 	List<RemitOrderModel> remitOrderList = remitOrderService.getRemitOrderList(keyword, start, pageSize);
		 	model.addAttribute("remitOrder",remitOrderList);
		 	model.addAttribute("totalCount",remitOrderService.remitOrderCount(keyword));
		  	model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/script_remit_order";
		}
	}
}